#ifndef __MP_HTTP_CLIENT_H__
#define __MP_HTTP_CLIENT_H__

#include "eventloop.h"
#include "tcp_client.h"
#include "http_request.h"
#include "http_response.h"
#include "http_client_context.h"

#include <functional>
#include <memory>

namespace Gazella {
struct IHttpRequestArg: public std::enable_shared_from_this<IHttpRequestArg> {
    virtual ~IHttpRequestArg() = default;
};

typedef std::function<void(const HttpRequest&, HttpResponse&, std::shared_ptr<IHttpRequestArg>)>  http_response_callback_t;

class HttpClient: public TcpClient {
public:
    HttpClient();
    ~HttpClient();

    //init
    int init(std::string host, int port) override;

    int request(const HttpRequest& request, http_response_callback_t response_func, std::shared_ptr<IHttpRequestArg> arg = nullptr, int timeout=3); 

private:
    void __recv_cb(ev::io &w, int revents) override;

    void __connect_callback() override;
    void __disconnect_callback() override;

    int __check_http_response();

    void __request_timer_callback(ev::timer &w, int revents);

private:
    HttpRequest m_http_request;
    http_response_callback_t m_response_func = nullptr;
    std::shared_ptr<IHttpRequestArg> m_data; //user data

    ev::timer m_request_timer;

    std::shared_ptr<HttpClientContext> m_httpclient_context;
};
}

#endif